package com.family.relation.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * Created by ljgsonx on 2016/1/23.
 */
public class StringUtil {

    /**
     * Description: <p>判断字符串是否为null,或是否为空字符串</p>
     * <p>null或空字符串:true</p>
     * Author: ljgsonx
     * Time: 2016/1/23 12:07
     */
    public static boolean isBlank(String str){
        if(str == null)
            return true;
        str = str.trim();
        if("".equals(str))
            return true;
        return false;
    }

    /**
     * Description: 判断传进来的所有参数,是否有null或有空字符串
     * Author: ljgsonx
     * Time: 2016/1/23 18:13
     */
    public static boolean hasBlank(String... str){
        for(String s:str){
            if(isBlank(s)){
                return true;
            }
        }
        return false;
    }
    
	public static String getJSON(Object o){
		return JSONObject.toJSONString(o,SerializerFeature.WriteMapNullValue);
	}
	
	public static String getJSONWithDate(Object o){
		return JSONObject.toJSONStringWithDateFormat(o, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
	}
	
	public static BigDecimal divide(BigDecimal divident, BigDecimal divisor){
		if(divisor.compareTo(new BigDecimal(0))==0){
			return new BigDecimal(0);
		}else{
			return divident.divide(divisor,3, BigDecimal.ROUND_HALF_UP);
		}
	}

    public static Date parseDate(String dateString) {
        Date date = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }
}
